/**
 * Created by WYH on 17/4/19.
 */
angular.module('tribalc').controller(
    'LocksController',
    ['$scope', 'Api', 'LockModel', 'Enum', 'Alert', function ($scope, Api, LockModel, Enum, Alert) {


        getLockList();
        $scope.isNature = true;

        Api.getCompanyDetail(Api.getAssign(), function (result) {
            if (!result.data.propertyNature) {
                //不是物业公司
                $scope.isNature = false
            }
        });

        $scope.showLockRent = false;
        $scope.lockRentContent = "确定把该门禁设备授权给访客";//'取消该门禁给方可的授权'
        $scope.lockListTitles = Enum.LockListTitle;
        $scope.lockRentAuth = lockRentAuth;
        $scope.rentManageAuth = rentManageAuth;
        // 跳转页面
        $scope.showLockOption = 1;
        $scope.selectHead = function (select) {

            $scope.selectItem = select;
            // 跳转页面
            $scope.showLockOption = checkLockSelected(select.name);
        };
        // 跳转页面
        $scope.showBottomLine = function (item) {
            if ($scope.showLockOption == 1) {
                $('#first_head').addClass('lock-click-bottom');
            } else {
                $('#first_head').removeClass('lock-click-bottom');
            }
            return {
                true: 'lock-click-bottom', false: ''
            }[item == $scope.selectItem]
        };

        // 跳转页面
        function checkLockSelected(option) {
            if (option == '设备管理') {

                return 1;
            } else if (option == '设备出入记录') {
                return 2;
            } else {
                return 3;
            }

        }

        $scope.getLockActive = function () {
            if ($scope.lockList.guestEnabled && $scope.lockList.guestEnabled == true) {
                return 'img/lock/little.png';
            }
        };
        function lockRentAuth(list) {
            $scope.showLockRent = true;
            if (list.guestEnabled == true)
            {
                $scope.lockRentContent = '取消该门禁给方可的授权';
            }
            else
            {
                $scope.lockRentContent = "确定把该门禁设备授权给访客";
            }
            $scope.rentManageAuthList = list.guestEnabled;
            // console.log($scope.rentManageAuthList);
            // console.log(list);
            $scope.currentEquipInfo = list;

            // Api.setManageAuthor(compantID,function () {
            //
            // })
        }

        function rentManageAuth() {
            // console.log(!$scope.rentManageAuthList)
            Api.setUpRentManageAuth(!$scope.rentManageAuthList, $scope.currentEquipInfo.id, function (result) {
                $scope.lockList.forEach(function(lock){
                    console.log(lock)
                  if(lock.id ==  $scope.currentEquipInfo.id){
                      lock.guestEnabled = !lock.guestEnabled
                  }
                });
                var alertContent = !$scope.rentManageAuthList ? "授权成功" : "取消授权成功";
                Alert.alertBox(alertContent, function () {
                    $scope.showLockRent = false;

                });
            })
        }


        function isShowUpdate() {
            $scope.showUpload = $scope.staffList.length !== 0;
        }


        function getLockList() {

            Api.getAuthLockList(function (result) {

                $scope.lockList = LockModel.setupLocks(result.data);
                // console.log($scope.lockList)
                $scope.showRentInf = $scope.lockList.length !== 0;
            });
        }


    }]
);